package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.ReconDetailQueryMapper;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.MchntReconDetail;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 对账单明细查询正式表接口类
 * Created by yuhongwen
 */
public class ReconDetailQueryMapperImpl implements ReconDetailQueryMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());


    @Override
    public List<MchntOrderInfo> selectMchntOrder(String instId, String mchntNo, String startDate, String endDate, String txnNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderReconDetail");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setRemark1(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark2(endDate);
        }

        if (null != txnNum && !txnNum.isEmpty()) {
            mchntOrderInfo.setTxnNum(txnNum);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrder(String instId, String mchntOrderId, String SysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderReconDetail");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        if (null != mchntOrderId && !mchntOrderId.isEmpty()) {
            payOrderInfo.setMchntOrderId(mchntOrderId);
        }
        if (null != SysSeqNum && !SysSeqNum.isEmpty()) {
            payOrderInfo.setSysSeqNum(SysSeqNum);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易明细查询失败");
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }


    @Override
    public MchntReconDetail selectMchntReconDetail(String instId, String mchntNo, String startDate, String endDate, String txnNum, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectReconDetail");
        MchntReconDetail mchntReconDetail = new MchntReconDetail();
        mchntReconDetail.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntReconDetail.setMchntNo(mchntNo);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntReconDetail.setStartDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntReconDetail.setEndDate(endDate);
        }
        if (null != txnNum && !txnNum.isEmpty()) {
            mchntReconDetail.setTxnNum(txnNum);
        }
        if (null != sysSeqNum && !sysSeqNum.isEmpty()) {
            mchntReconDetail.setSysSeqNum(sysSeqNum);
        }
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntReconDetail, mchntReconDetail);
        if (0 != iReturnCode) {
            rglog.error("对账单明细查询失败");
            return null;
        }
//        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
//        if (null == objectList || objectList.isEmpty()) {
//            rglog.error("订单信息查询失败");
//            return null;
//        }
        return mchntReconDetail;
    }

    @Override
    public PageResult selectMchntOrderReconDetailPage(String instId, String mchntNo, String startDate, String endDate, String txnNum, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderReconDetailPage");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != startDate && !startDate.isEmpty()) {
            mchntOrderInfo.setRemark1(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            mchntOrderInfo.setRemark2(endDate);
        }

        if (null != txnNum && !txnNum.isEmpty()) {
            mchntOrderInfo.setTxnNum(txnNum);
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class, pageInfo);
    }
}
